package com.xgz.inter;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //到达controller之前，判断session里面是否有值
        //获取session
        HttpSession session = request.getSession();
        //获取session里存的值
        Object name = session.getAttribute("uname");
        System.out.println("name = " + name);
        //判断name是否为空
        if (name == null){
            //为空  重定向于登录页面
            String contextPath = request.getContextPath();
            String path = contextPath+"login.jsp";
            response.sendRedirect(path);
            return false;
        }else{
            return true;
        }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
